<?php
/**
 * Created by PhpStorm.
 * User: woddp
 * Date: 2017/2/12
 * Time: 12:15
 */
namespace Admin\Controller;


use Think\Controller;

class ArticleController extends CommonController {

    public function index() {
        $category_id=I('get.category_id');
        $article_model=M('Article');
        $condition['category_id']=$category_id;
        $condition['status']=1;
        $ret=$article_model->where($condition)->select();
        $this->assign('articles',$ret);
        $this->display();
    }

    public function add() {
        if(IS_POST){
            $data=I('post.');
            $images=$data['images'];

            if(empty($data['title'])){
                return $this->ajaxReturn(['status'=>0,'msg'=>'标题名称不能为空']);
            }if(empty($data['category_id'])){
                return $this->ajaxReturn(['status'=>0,'msg'=>'请选择分类']);
            }if(strlen($data['title'])>80){
                return $this->ajaxReturn(['status'=>0,'msg'=>'标题名称过长！']);
            }

            $category_model=M('Article');
            $images_model=M('Images');
            $data['content']=htmlspecialchars($data['content']);
            $id=$category_model->data($data)->add();
            $insert_arr=[];
            foreach($images as $list){
                $insert_arr[]=[
                    'pid'=>$id,
                    'path'=>$list
                ];
            }
            $images_id=$images_model->addAll($insert_arr);
            if($id){
                return $this->ajaxReturn(['status'=>1,'msg'=>'文章添加成功','url'=>U('article/index',['category_id'=>$data['category_id']])]);
            }else{
                return $this->ajaxReturn(['status'=>0,'msg'=>'数据添加失败请重试！']);
            }
        }else{
            $category_model=M('Category');
            $ret=$category_model->field('id,name')->select();
            $this->assign('category',$ret);
            $this->display();
        }

    }

    public function edit() {
        if(IS_POST){
            $data=I('post.');

            if(empty($data['title'])){
                return $this->ajaxReturn(['status'=>0,'msg'=>'标题名称不能为空']);
            }if(empty($data['category_id'])){
                return $this->ajaxReturn(['status'=>0,'msg'=>'请选择分类']);
            }if(strlen($data['title'])>80){
                return $this->ajaxReturn(['status'=>0,'msg'=>'标题名称过长！']);
            }
            $article_model=M('Article');
            $status1=$article_model->where("id={$data['id']}")->save($data);
            if($status1){
                return $this->ajaxReturn(['status'=>1,'msg'=>'文章更新成功','url'=>U('article/index',['category_id'=>$data['category_id']])]);
            }else{
                return $this->ajaxReturn(['status'=>0,'msg'=>'文章更新失败请重试！']);
            }
        }else{
            $id=I('get.id');
            if(empty($id)){
                $this->error('参数错误！');
            }
            $article_model=M('Article');
            $category_model=M('Category');
            $images_model=M('Images');

            $ret_article=$article_model->where("id={$id}")->find();
            $this->assign('val',$ret_article);

            $ret_category=$category_model->field('id,name')->select();
            $this->assign('category',$ret_category);


            $ret_images=$images_model->where("pid={$id}")->select();
            $this->assign('images',$ret_images);
            $this->assign('category',$ret_category);

            $this->display('update');
        }
    }

    public function del() {
        $id=I('post.id');

        if(empty($id)){
            $this->error('参数错误！');
        }
        $category_model=M('Article');
         $category_model->status='0';
        $status1=$category_model->where("id={$id}")->save();
        if($status1){
            return $this->ajaxReturn(['status'=>1,'msg'=>'删除成功']);
        }else{
            return $this->ajaxReturn(['status'=>0,'msg'=>'删除失败']);
        }
    }
    
}